import { Injectable, Logger, NestMiddleware } from '@nestjs/common';
import { NextFunction } from 'express';
import { encrypt, getSalt } from 'src/utils/encryption';

const logger = new Logger('hash-password.middleware');

@Injectable()
export class HashPasswordMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction) {
    let userPassword = req.body['password'];
    const salt = getSalt();
    userPassword = encrypt(userPassword, salt);
    req.body['password'] = userPassword;
    req.body['salt'] = salt;

    next();
  }
}
